//
//  EnterActivityInforViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2018/1/31.
//  Copyright © 2018年 史宵宵. All rights reserved.
//

#import "EnterActivityInforViewController.h"
#import "PayActivityInforViewController.h"

@interface EnterActivityInforViewController ()<UITextFieldDelegate,UIGestureRecognizerDelegate>
@property (nonatomic,assign) BOOL isSending;

@end

@implementation EnterActivityInforViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.moneyLabel.text = [NSString stringWithFormat:@"合计  ¥%@",self.moneyStr];
    self.nameTextField.delegate = self;
    self.telTextField.delegate = self;
    self.isSending = NO;
    //增加监听，当键盘出现或改变时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    //增加监听，当键退出时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disSeleteMood:)];
    tap.delegate=self;
    [self.view addGestureRecognizer:tap];
    self.view.userInteractionEnabled = YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if (touch.view==self.view) {
        return YES;
    }
    else
        return NO;
}
- (void)disSeleteMood:(UITapGestureRecognizer *)tap{
    [self.view endEditing:YES];
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (IBAction)disViewController:(id)sender {
    [self.view endEditing:YES];
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (IBAction)nextStep:(id)sender {
    [self.view endEditing:YES];
    if (strIsEmpty(self.nameTextField.text)) {
        [Global showActivityWithView:self.view withText:@"请输入姓名"];
        return;
    }
    if (strIsEmpty(self.telTextField.text)) {
        [Global showActivityWithView:self.view withText:@"请输入手机号码"];
        return;
    }
    
    if (self.isSending) {
        return;
    }
    self.isSending = YES;
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户认证
    [dic setValue:self.nameTextField.text forKey:@"name"]; //姓名
    [dic setValue:self.telTextField.text forKey:@"phone"]; //手机号
    [dic setValue:self.activityInfo.fee forKey:@"amount"]; //金额单位（元）
//    [dic setValue:@"0.01" forKey:@"amount"]; //金额单位（元）
    [dic setValue:self.numStr forKey:@"num"]; //数量

    [yjNetWorkManger requestType:POST andURL: [JSPathUtil urlAppPaySaveUserOrder] andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            self.isSending = NO;
            [self dismissViewControllerAnimated:NO completion:^{
                
            }];
            PayActivityInforViewController *payActivityInforVC = [[PayActivityInforViewController alloc]init];
            payActivityInforVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
            payActivityInforVC.moneyStr = self.moneyStr;
            payActivityInforVC.orderId = [NSString stringWithFormat:@"%@",response];
            payActivityInforVC.activityInfo = self.activityInfo;
            payActivityInforVC.num = self.numStr;
            UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
            [appRootVC presentViewController:payActivityInforVC animated:YES completion:^{
        
            }];
        }
        self.isSending = NO;
    }];
    

}
#pragma mark ------键盘操作
//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
    //获取键盘的高度
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    int height = keyboardRect.size.height;
    self.view.frame = CGRectMake(0, - height, kScreenW, kScreenH);
}

//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification{
    self.view.frame = CGRectMake(0, 0, kScreenW, kScreenH);
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    [textField endEditing:YES];
    return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    [textField endEditing:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
